package com.yc;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetLocalIP {
    public static void main(String[] args) {


        try {
            // 取得本地主机
            InetAddress localhost = InetAddress.getLocalHost();
            // 获取主机名
            String hostname = localhost.getHostName();
            // 获取IP地址的字符串形式
            String ip = localhost.getHostAddress();
            System.out.println("主机名: " + hostname);
            System.out.println("IP地址: " + ip);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}